<template>
  <a-card :bordered="false">
    <div class="table-operator">
      <a-button type="primary" icon="redo" @click="getDataList()">刷新</a-button>
    </div>

    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="10">
          <a-col :md="4" :sm="24">
            <a-form-item label="查询类别">
              <a-select allowClear v-model="queryParam[0].condition">
                <a-select-option key="CustomerName">客户名称</a-select-option>

                <!-- <a-select-option key="DeliverAddress">发货地址</a-select-option> -->
                <a-select-option key="DocumentNo">单据号</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="24">
            <a-form-item>
              <a-input v-model="queryParam[0].Keyword" placeholder="关键字" @keydown.enter="getDataList"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button
              type="primary"
              @click="
                () => {
                  this.pagination.current = 1
                  this.getDataList()
                }
              "
            >查询
            </a-button
            >
            <a-button style="margin-left: 8px" @click="repossess">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table
      ref="table"
      :scroll="{ y: 530 }"
      :columns="columns"
      :rowKey="(row, index) => row.Id"
      :dataSource="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      :bordered="true"
      size="small"
    >
      <template slot="SampleStatus" slot-scope="text, record, index">
        <a-tag color="blue" v-if="text == 0 || text == null"> 未试用</a-tag>
        <a-tag color="green" v-if="text == 1"> 试用成功</a-tag>
        <a-tag color="red" v-if="text == 2"> 试用失败</a-tag>
      </template>
      <span slot="CreateTime" slot-scope="CreateTime">{{ timeFormat(CreateTime) }}</span>
      <span slot="action" slot-scope="text, record">
        <template>
          <a v-if="record.SampleStatus == 0 || record.SampleStatus == null" @click="feedback(record.Id)">反馈</a>
          <a-divider v-if="record.SampleStatus == 0 || record.SampleStatus == null" type="vertical"/>

          <a @click="Check(record.Id)">查看</a>
        </template>
      </span>
    </a-table>
    <Delivery :parentObj="this" ref="Delivery"/>
    <Approval :parentObj="this" ref="Approval"/>
    <feedbackEdit :parentObj="this" ref="feedbackedit"/>

    <edit-form ref="editForm" :disabled="disabled" :parentObj="this"></edit-form>
  </a-card>
</template>

<script>
import Delivery from '../MySample/Delivery.vue'
import Approval from './Approval.vue'
import feedbackEdit from './feedbackEdit.vue'

import EditForm from './EditForm'
import moment from 'moment'

const columns = [
  { title: '客户名称', dataIndex: 'CustomerName', width: '20%' },
  { title: '单据号', dataIndex: 'DocumentNo' },
  {
    title: '申请日期',
    dataIndex: 'CreateTime',

    align: 'center',
    scopedSlots: { customRender: 'CreateTime' }
  },
  // { title: '销售经理', dataIndex: 'AccountManager' },
  // { title: '组织机构', dataIndex: 'DepartmentId' },
  { title: '经办人', dataIndex: 'Transactor' },
  // { title: '发货状态', dataIndex: 'DeliverStatus' },
  // { title: '快递单号', dataIndex: 'TrackingNumber' },
  { title: '试用说明', dataIndex: 'SampleTrialRemark', width: '20%' },

  {
    title: '试用状态',
    dataIndex: 'SampleStatus',
    align: 'center',
    scopedSlots: { customRender: 'SampleStatus' }
  },
  { title: '操作', dataIndex: 'action', align: 'left', scopedSlots: { customRender: 'action' } }
]

export default {
  props: {
    parentObj: Object
  },
  components: {
    EditForm,
    Delivery,
    Approval,
    feedbackEdit
  },
  mounted () {
    this.$nextTick(() => {
      // 页面渲染完成后的回调
      this.tableHeight = this.getTableHeight(true) + 24
    })
    this.getUserList()
  },
  data () {
    return {
      disabled: false,
      data: [],
      List: [],
      pagination: {
        current: 1,
        pageSize: 25,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['25', '50', '100'], // 设置pageSize的可选值，页面可以通过下拉框进行选择
        showSizeChanger: true
      },
      filters: {},
      sorter: { field: 'CreateTime', order: 'desc' },
      loading: false,
      columns,
      queryParam: [{ condition: 'CustomerName' }, { condition: 'DeliverStatus', Keyword: 2 }],
      selectedRowKeys: []
    }
  },
  methods: {
    getUserList () {
      this.selectedRowKeys = []
      this.loading = true
      this.$http.post('/Base_Manage/Base_User/GetDataList', {}).then(resJson => {
        this.loading = false
        this.List = resJson.Data
        this.getDataList()
      })
    },
    CheckboxProps (record) {
      return {
        props: {
          disabled: record.ApprovalStatus == 1
        }
      }
    },
    repossess () {
      this.queryParam = [{ condition: 'CustomerName' }, { condition: 'DeliverStatus', Keyword: 2 }]
    },
    handleTableChange (pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }
      this.getDataList()
    },
    getDataList () {
      this.selectedRowKeys = []
      this.loading = true
      this.$http
        .post('/SampleManagement/SPL_SampleApplyMain/GetDataListByMySelfAndMoreCondition', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order,
          Search: this.queryParam,
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          this.data.forEach(ele => {
            var id = this.List.findIndex(item => {
              return item.Id == ele.Transactor
            })
            if (id !== -1) {
              ele.Transactor = this.List[id].RealName
            }
          })
          if (this.data) {
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
        })
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    hasSelected () {
      return this.selectedRowKeys.length > 0
    },
    handleAdd () {
      this.disabled = false
      this.$refs.editForm.openForm(null, '新建样品申请信息')
    },

    handleRevise (id) {
      this.disabled = false
      this.$refs.editForm.openForm(id, '修改')
    },
    handleEdit (id) {
      this.disabled = false
      this.$refs.editForm.openForm(id, '编辑样品申请信息')
    },
    feedback (id) {
      this.disabled = true
      this.$refs.feedbackedit.openForm(id, '反馈')
    },
    Check (id) {
      this.disabled = true
      this.$refs.editForm.openForm(id, '查看')
    },

    handleDelete (ids) {
      var thisObj = this
      this.$confirm({
        title: '确认删除吗?',
        onOk () {
          return new Promise((resolve, reject) => {
            thisObj.$http.post('/SampleManagement/SPL_SampleApplyMain/DeleteDataOfAll', ids).then(resJson => {
              resolve()

              if (resJson.Success) {
                thisObj.$message.success('操作成功!')

                thisObj.getDataList()
              } else {
                thisObj.$message.error(resJson.Msg)
              }
            })
          })
        }
      })
    },
    timeFormat (val) {
      // 时间格式化函数
      return moment(val).format('YYYY-MM-DD')
    }
  }
}
</script>
